import {LoginParam, doLogin} from "@/services/login-service";
import {useStore} from "@/store";
import { setToken,getRefreshToken,setRefreshToken } from '@/utils/cookies'

// useStore()?.state?.user?.token;
/**
 * 刷新token
 */
export function refreshToken() {
    return new Promise((resolve, reject) => {
        //获取 refresh_token
        const req = {grant_type: 'refresh_token', refresh_token: getRefreshToken()} as LoginParam;
        doLogin(req).then(({data}) => {
            const {access_token, token_type, refresh_token} = data;
            const accessToken = token_type + ' ' + access_token;
            console.log(accessToken);
            //重现赋值token
            setToken(accessToken);
            setRefreshToken(refresh_token);
            resolve(accessToken);
        }).catch(error => {
            reject(error)
        })
    })
}
